<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function List(arr) {
	 this.$_$ = arr || new Array();
	 this.length = this.$_$.length;
	 this.add = List.$add;
	 this.remove = List.$remove;
	 this.toString = List.$toString;

	}

	List.$add = function(m,i) {
	 if (('number' != typeof i)||(i >= this.$_$.length)) {
	  this.$_$.push(m);
	 }
	 else if ((i<0)||(parseInt(i,10)!=i)){
	  throw new Error('Invalid index');
	  // as a "problem hiding" option can be implied:
	  // i = Math.abs(parseInt(i,10));
	 }
	 else {
	  this.$_$.splice(i,0,m);
	 }
	 this.length = this.$_$.length;

	}

	List.$remove = function(i) {
	 if (('number' != typeof i)||(i >= this.$_$.length)) {
	  return null;
	 }
	 else if (parseInt(i,10) != i){
	  throw new Error('Invalid index');
	  // as a "problem hiding" option can be implied:
	  // i = (parseInt(i,10);
	 }
	 else {
	  if (i<0) {i-= this.$_$.length;}
	  var ret = this.$_$.splice(i,1)[0];
	  this.length = this.$_$.length;
	  return ret;
	 }

	}

	List.$toString = function() {
	 return this.$_$.toString();

	}

	// Create new vector and use it as a wrapper
	// over Array argument:
	var myList = new List(['Apples', 'Oranges', 'Bananas']);

	// add(newElement, atPosition) method
	// if no atPosition provided, newElement will
	// be added to the top
	myList.add('Mangos'); // add 'Mangos' to the top

	// Add 'Strawberries' atPosition 3
	// The element currently located at myList[3] and all elements
	// atop of it will be moved one position up
	myList.add('Strawberries', 3);

	// remove(atPosition) method
	// Elements currently located atop of it
	// will be moved one position down
	myList.remove(1); // remove 'Oranges'

	// length property shows the actual length
	window.alert(myList.length); // 4

	// toString method is overloaded so in string
	// context it gives comma-separated values
	window.alert(myList); // Apples,Bananas,Strawberries,Mangos 
</script>
</head>
<body>

</body>
</html>